/**    
 * @Description: 博客园地址：https://www.cnblogs.com/zumengjie/      
 * @author: 顶风少年   
 * @date: 2021年4月5日 下午10:30:14
 */
package datang.dtdb.scripting;

import datang.dtdb.builder.SqlSourceBuilder;
import datang.dtdb.builder.StaticSqlSource;
import datang.dtdb.mapping.BoundSql;
import datang.dtdb.mapping.SqlSource;
import datang.dtdb.session.Configuration;

/**
 * 只包含#{}这样的sql每次都一样再处理上只处理一次。
 */
public class RawSqlSource implements SqlSource {

	private SqlSource sqlSource;
	
	private Configuration configuration;

	public RawSqlSource(Configuration configuration, MixedSqlNode mixedSqlNode) {
		// 获取StaticSqlSource
		SqlSourceBuilder sqlSourceBuilder = new SqlSourceBuilder();
		String sql = getSql(mixedSqlNode);
		sqlSource = sqlSourceBuilder.parse(sql);
		this.configuration = configuration;
	}

	private String getSql(MixedSqlNode mixedSqlNode) {
		DynamicContext dynamicContext = new DynamicContext(configuration, null);
		mixedSqlNode.apply(dynamicContext);
		return dynamicContext.getSql();
	}

	@Override
	public BoundSql getBoundSql(Object parameterObject) {
		return sqlSource.getBoundSql(parameterObject);
	}

}
